<template>
  <div>
    <!-- 1. 顶部栏 -->
    <div class="header">
      <van-image width="60" height="60" fit="cover" round :src="userInfo.photo" />
      <h2>
        {{userInfo.name}}
        <br />
        <van-tag color="#fff" text-color="#009afb">{{userInfo.birthday}}</van-tag>
      </h2>
    </div>
    <!-- 2. 操作链接 -->
    <van-row>
      <van-col span="8">
        <van-icon name="todo-list-o" color="#2eadff" />
        <span>我的作品</span>
      </van-col>
      <van-col span="8">
        <van-icon name="star-o" color="#ca3b57" />
        <span>我的收藏</span>
      </van-col>
      <van-col span="8">
        <van-icon name="tosend" color="#dbc380" />
        <span>阅读历史</span>
      </van-col>
    </van-row>
    <!-- 3. cell列表 -->
    <van-cell-group>
      <van-cell title="编辑资料" icon="edit" is-link @click="$router.push('./edit')"/>
      <van-cell title="小智同学" icon="chat-o" is-link @click="$router.push('./student')" />
      <van-cell title="系统设置" icon="setting-o" is-link />
      <van-cell title="退出登录" icon="warning-o" is-link @click="logout" />
    </van-cell-group>
  </div>
</template>

<script>
// 导入接口
import { getUserInfo } from "./user.js";
import { removeToken } from "@/utils/token.js";

export default {
  name: "user",
  data() {
    return {
      userInfo: {}
    };
  },
  created() {
    // 1.请求个人信息
    getUserInfo().then(res => {
      console.log(res);
      // (1)绑定数据
      this.userInfo = res.data.data;
      // (2)更新到vuex
      this.$store.commit('updateUserInfo',res.data.data)
    });
  },
  methods: {
    logout() {
      this.$dialog
        .confirm({
          title: "提示",
          message: "确定要退出登录吗？"
        })
        .then(() => {
          // 点击确定：退出登录
          // （1）清除token
          removeToken()
          // （2）跳转登录页
          this.$router.push('/login')
        })
        .catch(() => {
          // on cancel
        });
    }
  }
};
</script>

<style lang="less">
// 1. 顶部栏
.header {
  height: 100px;
  background-color: #009afb;
  // 伸缩盒子
  display: flex;
  // 垂直居中
  align-items: center;

  .van-image {
    padding: 0px 10px;
  }

  h2 {
    font-size: 18px;
    color: #fff;
    font-weight: normal;
  }
}

.van-row {
  .van-col {
    margin: 10px 0;
    display: flex;
    flex-direction: column;
    align-items: center;

    span {
      font-size: 12px;
    }
  }
}
</style>